﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace NEWS.Admin
{
    public partial class Edit_AddNewsBussiness : System.Web.UI.Page
    {
        private static string mode = null, modeapprove = null;
        private static NewsBussinessBO newsbussinessbo;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                mode = Request.QueryString["Mode"];
                modeapprove = Request.QueryString["approve"];
                if (mode != null)
                {
                    //quy dinh an hien cac truong
                    pn_iduser.Visible = false;
                    pn_songaymuondang.Visible = false;
                    pn_giahan.Visible = false;
                    newsbussinessbo = NewsBussinessBO.GetNewsBussinessBO(Convert.ToInt32(mode));
                    if (newsbussinessbo.IsExtension == false)
                    {
                        pn_ngaygiahagannhat.Visible = false;
                    }
                    //lay option
                    OptionBO optionbo;
                    optionbo = OptionBO.GetNameOptionBO("SoLanGiaHanTinDoanhNghiep");
                    if (modeapprove == "true")
                    {
                        lblapprove.Visible = true;
                        btbupdate.Focus();
                    }
                    if (newsbussinessbo.ExpireDate <= DateTime.Now && Convert.ToInt32(optionbo.OptionValue) > newsbussinessbo.NumExtension)
                    {
                        pn_checkgiahan.Visible = true;
                        btbupdate.Visible = false;
                        lblsolancon.Text = (Convert.ToInt32(optionbo.OptionValue) - newsbussinessbo.NumExtension).ToString();                        
                    }
                    if (Convert.ToInt32(optionbo.OptionValue) <= newsbussinessbo.NumExtension)
                    {
                        pn_checkgiahan.Visible = true;
                        lblthongbaohethan.Text = " Tin này đã hết hạn và hết quyền gia hạn  ";
                        lbtgiahan.Text = "Click Vào Đây Đăng Tin Mới";
                        btbupdate.Visible = false;
                    }
                    
                    txtnewsbussinessid.Text = newsbussinessbo.NewsBussinessID.ToString();
                    txtusername.Text = newsbussinessbo.UserName;
                    txtbussinessname.Text = newsbussinessbo.BussinessName;
                    txtbussinessaddress.Text = newsbussinessbo.BussinessAddress;
                    txtwebsite.Text = newsbussinessbo.BussinessWebsite;
                    txtphone.Text = newsbussinessbo.BussinessPhone;
                    txtbussinesscontent.Text = newsbussinessbo.BussinessContent;
                    txtemail.Text = newsbussinessbo.Email;
                    lbldatecreated.Text = Convert.ToDateTime(newsbussinessbo.CreatedDate).ToString("dd/MM/yyyy");
                    txtexpiredate.Text = Convert.ToDateTime(newsbussinessbo.ExpireDate).ToString("dd/MM/yyyy");
                    txtdateextension.Text = Convert.ToDateTime(newsbussinessbo.ExtensionDate).ToString("dd/MM/yyyy");
                    //lbldatepost.Text = newsbussinessbo.PostDate.ToString();
                    lbldateupdate.Text = Convert.ToDateTime(newsbussinessbo.UpdatedDate).ToString("dd/MM/yyyy");
                    txtnumextention.Text = newsbussinessbo.NumExtension.ToString();
                    txtsolandagiahan.Text = newsbussinessbo.NumExtension.ToString();
                    isactive.Checked = newsbussinessbo.IsActive;
                }
                else
                {
                    pn_iduser.Visible = false;
                    pn_suatin.Visible = false;
                    pn_giahan.Visible = false;
                    pn_songaymuondang.Visible = true;
                    lblheader.Text = "Đăng Tin Doanh Nghiệp";
                    btbupdate.Text = "Thêm";
                }
            }
        }

        protected void btbupdate_Click(object sender, EventArgs e)
        {
            if (mode != null)
            {
                //thuc hien code cho update
                int newsbussinessid = Convert.ToInt32(txtnewsbussinessid.Text);
                newsbussinessbo = NewsBussinessBO.GetNewsBussinessBO(newsbussinessid);
                newsbussinessbo.BussinessName = txtbussinessname.Text;
                newsbussinessbo.BussinessPhone = txtphone.Text;
                newsbussinessbo.BussinessAddress = txtbussinessaddress.Text;
                newsbussinessbo.BussinessWebsite = txtwebsite.Text;
                newsbussinessbo.BussinessContent = txtbussinesscontent.Text;
                newsbussinessbo.Email = txtemail.Text;
                newsbussinessbo.UpdatedDate = DateTime.Now;
                newsbussinessbo.IsActive = isactive.Checked;
                //cap nhat khi co gia han
                if (pn_giahan.Visible == true)
                {
                    newsbussinessbo.IsExtension = true;
                    newsbussinessbo.NumExtension += 1;
                    txtnumextention.Text = newsbussinessbo.NumExtension.ToString();
                    newsbussinessbo.ExpireDate = DateTime.Now.AddDays(double.Parse(drlgiahan.SelectedValue));
                    newsbussinessbo.PostDate = DateTime.Now;
                    txtexpiredate.Text = DateTime.Now.AddDays(double.Parse(drlgiahan.SelectedValue)).ToString();
                    newsbussinessbo.ExtensionDate = DateTime.Now;
                    try
                    {
                        newsbussinessbo.Save();
                        pn_giahan.Visible = false;
                        pn_checkgiahan.Visible = false;
                        lbldateupdate.Text = DateTime.Now.ToString();
                        message.Text = "Cập nhật thành công !";
                    }
                    catch
                    {
                        message.Text = "Cập nhật thất bại !";
                    }

                }
                else
                {
                    try
                    {
                        newsbussinessbo.Save();
                        if (newsbussinessbo.ExpireDate <= DateTime.Now)
                        {

                        }
                        lbldateupdate.Text = DateTime.Now.ToString();
                        if (modeapprove == "true")
                        {
                            if (isactive.Checked == true)
                                Response.Redirect("NewsBussiness.aspx?Save=Complete&approve=true");
                            else Response.Redirect("NewsBussiness.aspx?approve=true");
                        }
                        else Response.Redirect("NewsBussiness.aspx?Save=Complete"); 
                    }
                    catch
                    {
                        message.Text = "Cập nhật thất bại !";
                    }
                }
            }
            else
            {
                //Them moi tin doanh nghiep
                newsbussinessbo = NewsBussinessBO.NewNewsBussinessBO();
                newsbussinessbo.UserName = "admin";
                newsbussinessbo.BussinessName = txtbussinessname.Text;
                newsbussinessbo.BussinessAddress = txtbussinessaddress.Text;
                newsbussinessbo.BussinessWebsite = txtwebsite.Text;
                newsbussinessbo.BussinessPhone = txtphone.Text;
                newsbussinessbo.BussinessContent = txtbussinesscontent.Text;
                newsbussinessbo.Email = txtemail.Text;
                newsbussinessbo.IsExtension = false;
                newsbussinessbo.IsActive = isactive.Checked;
                newsbussinessbo.CreatedDate = DateTime.Now;
                newsbussinessbo.UpdatedDate = DateTime.Now;
                newsbussinessbo.NumExtension = 0;
                newsbussinessbo.PostDate = DateTime.Now;
                newsbussinessbo.ExpireDate = DateTime.Now.AddDays(double.Parse(drlnumexpire.SelectedValue));
                try
                {
                    newsbussinessbo.Save();
                    message.Text = "Thêm thành công !";
                    Response.Redirect("NewsBussiness.aspx?Save=Complete");
                    //resetControls(); dua tat ca cac textbox ve null
                }
                catch
                {
                    message.Text = "Thêm thất bại !";
                }
            }
        }
        protected void btnback_Click(object sender, EventArgs e)
        {
            Response.Redirect("NewsBussiness.aspx");
        }

        protected void lbtgiahan_Click(object sender, EventArgs e)
        {
            if (lbtgiahan.Text == "Click Vào Đây Đăng Tin Mới")
            {
                Response.Redirect("Edit_AddNewsBussiness.aspx");
            }
            else
            {
                pn_giahan.Visible = true;
                btbupdate.Visible = true;
                lbtgiahan.Focus();
            }
        }
    }
}
